package assignment2;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class GameUI implements ActionListener {
    /*Instance Variables*/
    private JFrame window = new JFrame("N-in-a-row");
    private JButton buttons[][];
    private String letter = "";
    private boolean win = false;
    private GameBoardOld board;
    private int width,height,goal;

    private class MyButton extends JButton {
        public int x,y;
        public MyButton(int x,int y){
            this.x=x;
            this.y=y;
        }
    }

    public GameUI(int width,int height,int goal){
        this.width=width;
        this.height=height;
        this.goal=goal;
        board=new GameBoardOld(width,height,goal);
        /*Create Window*/
        window.setSize(75 * width, 75 * height + 25);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new GridLayout(height,width));

	    JMenuBar JMB = new JMenuBar();
	    window.setJMenuBar(JMB);
	    JMenu file = new JMenu("File");
        JMB.add(file);
        JMenu game = new JMenu("Game");
        game.add(NewGame);
        game.add(GravityOn);
        game.add(GravityOff);
        JMB.add(game);

        /*Add Buttons To The Window*/
        buttons=new MyButton[width][];
        for(int i=0; i< width; i++){
            buttons[i]=new MyButton[height];
        }
        for(int j=0;j<height;j++){
            for(int i=0; i< width; i++){
                buttons[i][j] = new MyButton(i,j);
                window.add(buttons[i][j]);
                buttons[i][j].setText(i+"x"+j);
                buttons[i][j].addActionListener(this);
            }
        }
        /*Make The Window Visible*/
        UpdateBoard();
        window.setVisible(true);
    }
    
    public void poll(){
        if(board.dropping()){
            board.update();
            UpdateBoard();
            if(board.gameOver()){
                JOptionPane.showMessageDialog(null,board.status());
            }
        }
    }

    /**
     When an object is clicked, perform an action.
     @param a action event object
     */
    public void actionPerformed(ActionEvent a) {
        /*Write the letter to the button and deactivate it*/
        MyButton pressedButton = (MyButton)a.getSource();
        board.move(pressedButton.x,pressedButton.y);
        UpdateBoard();
        /*Show a dialog when game is over*/
        if(board.gameOver()){
            JOptionPane.showMessageDialog(null,board.status());
        }
    }
    private void UpdateBoard(){
        for(int i=0;i<width;i++){
            for(int j=0;j<height;j++){
                switch(board.field(i,j)){
                    case GameBoardOld.Empty:
                        buttons[i][j].setEnabled(true);
                        buttons[i][j].setText(" ");
                        break;
                    case GameBoardOld.Cross:
                        buttons[i][j].setEnabled(false);
                        buttons[i][j].setText("X");
                        break;
                    case GameBoardOld.Circle:
                        buttons[i][j].setEnabled(false);
                        buttons[i][j].setText("O");
                        break;
                }
            }
        }
        window.setTitle(goal+"-in-a-row - "+board.status());
    }
    
    public static void main(String[] args){
        GameUI game = new GameUI(7,6,4); // 4-in-a-row
        //GameUI game = new GameUI(3,3,3); // tic-tac-toe
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e){}
            game.poll();
        }
    }

    Action Quit = new AbstractAction("Quit") {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	};
    Action NewGame = new AbstractAction("New Game") {
		public void actionPerformed(ActionEvent e) {
			board.clearboard();
            UpdateBoard();
		}
	};
    Action GravityOn = new AbstractAction("Enable Gravity") {
		public void actionPerformed(ActionEvent e) {
			board.setGravity(true);
		}
	};
    Action GravityOff = new AbstractAction("Disable Gravity") {
		public void actionPerformed(ActionEvent e) {
			board.setGravity(false);
		}
	};

}


